package com.music.detail.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.music.detail.dao.VedioDetailDao;
import com.music.domain.Commit;
import com.music.domain.Video;
import com.music.domain.VideoCommit;

@Repository
public class VedioDetailDaoImpl implements VedioDetailDao {
	
	@Autowired
	SessionFactory sessionFactory;
	@Override
	public Video findVideoById(String videoId) {
		Video video = sessionFactory.getCurrentSession().get(Video.class, videoId);
		return video;
	}
	@Override
	public List<VideoCommit> findCommitsById(String videoid) {
		String hql="from VideoCommit where videoid=:videoid";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setParameter("videoid", videoid);
		List<VideoCommit> data = query.list();
		return data;
	}
	
	
	@Override
	public void addCommit(Map<String, Object> param) {
		String sql="INSERT INTO T_VIDEO_COMMIT(ID,TEXT,VIDEOID,CREATENAME)"
				+ " VALUES(:ID,:TEXT,:VIDEOID,:CREATENAME)";
		SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("ID", param.get("id"));
		query.setParameter("TEXT", param.get("text"));
		query.setParameter("VIDEOID", param.get("videoid"));
		query.setParameter("CREATENAME", param.get("createname"));
		query.executeUpdate();
	}

}
